﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;
using MxWeiXinPF.BLL;

namespace MxWeiXinPF.Web.admin.toupiao
{
    /**************************************
*
* author:李朴
* company:上海沐雪网络科技有限公司
* qq:23002807
* website:http://uweixin.cn
* taobao:http://uweixin.cn/portalpage/weixin_news_detail.aspx?id=2102
* createDate:2013-11-1
* update:2014-12-30
* 
***********************************/

    public partial class action_edit : Web.UI.ManagePage
    {
        //1e2124dd04e11d01b9df2865f85944be
        private string action = MXEnums.ActionEnum.Add.ToString(); //操作类型

        wx_tp_action actBll = new wx_tp_action();
      
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = MXRequest.GetQueryString("action");
            int id = 0;
            if (!string.IsNullOrEmpty(_action) && _action == MXEnums.ActionEnum.Edit.ToString())
            {

                this.action = MXEnums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out  id))
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!actBll.Exists(id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                TypeBind();

                if (action == MXEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(id);
                }
                else
                {
                    txtstartDate.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    txtendDate.Text = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss");
                }
            }
        }

        /// <summary>
        /// 绑定类别
        /// </summary>
        private void TypeBind()
        {
            Model.wx_userweixin weixin = GetWeiXinCode();

            BLL.wx_albums_info bll = new BLL.wx_albums_info();
            IList<Model.wx_albums_info> atlist = bll.GetModelList("wid=" + weixin.id + " order by seq asc");


            this.ddlAlbumsCategory.Items.Clear();
            this.ddlAlbumsCategory.Items.Add(new ListItem("请选择相册", "0"));
            foreach (Model.wx_albums_info albums in atlist)
            {
                this.ddlAlbumsCategory.Items.Add(new ListItem(albums.aName, albums.id.ToString()));
            }

        }



        #region 赋值操作=================================
        //1e2124dd04e11d01b9df2865f85944be
        private void ShowInfo(int id)
        {
            hidid.Value = id.ToString();
            Model.wx_tp_action actAction = actBll.GetModel(id);


            txtname.Text = actAction.name;
            txtstartDate.Text = actAction.startDate.Value.ToString("yyyy-MM-dd HH:mm:ss"); ;
            txtendDate.Text = actAction.endDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
            ddlAlbumsCategory.SelectedValue = actAction.albumsTypeId.Value.ToString();
            txtactContent.Value = actAction.actContent;
            txtremark.Value = actAction.remark;
            txttpTimes.Text =actAction.tpTimes==null?"0":actAction.tpTimes.ToString();

            txtfxPic.Text = actAction.fxPic;
            //结束
            if (actAction.fxPic != null )
            {

                imgfxPic.ImageUrl = actAction.fxPic;
            }
            txtfxTitle.Text = actAction.fxTitle;

            txtfxContent.Value = actAction.fxContent;
        }

        #endregion



        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.wx_userweixin weixin = GetWeiXinCode();

           
            int id = MyCommFun.Str2Int(hidid.Value);
            #region  //先判断
            string strErr = "";

            if (this.txtname.Text.Trim().Length == 0)
            {
                strErr += "活动名称不能为空！";
            }
            if (this.txtstartDate.Text.Trim().Length == 0 || !MyCommFun.isDateTime(txtstartDate.Text))
            {
                strErr += "开始时间不能为空！";
            }
            if (this.txtendDate.Text.Trim().Length == 0 || !MyCommFun.isDateTime(txtendDate.Text))
            {
                strErr += "结束时间不能为空！";
            }
            

            if (strErr != "")
            {
                JscriptMsg(strErr, "back");
                return;
            }
            DateTime begin = DateTime.Parse(txtstartDate.Text.Trim());
            DateTime end = DateTime.Parse(txtendDate.Text.Trim());
            if (begin >= end)
            {
                JscriptMsg("开始时间必须小于结束时间", "back");
                return;
            }
            #endregion

            #region 赋值
            Model.wx_tp_action actModel = new Model.wx_tp_action();
          
           
            if (id > 0)
            {
                actModel = actBll.GetModel(id);
            }

            actModel.name = txtname.Text.Trim();
           
            actModel.startDate = begin;
            actModel.endDate = end;
            actModel.actContent = txtactContent.Value.Trim();
            actModel.albumsTypeId = MyCommFun.Str2Int(  ddlAlbumsCategory.SelectedItem.Value);
            actModel.actContent = txtactContent.Value.ToString();
            actModel.remark = txtremark.Value.ToString();

            actModel.fxPic = txtfxPic.Text.ToString();
            actModel.fxTitle = txtfxTitle.Text.ToString();
            actModel.fxContent = txtfxContent.Value.ToString();
            actModel.tpTimes = MyCommFun.Str2Int(txttpTimes.Text);

            #endregion

            if (id <= 0)
            {  //新增
                actModel.wid = weixin.id;
                actModel.createdate = DateTime.Now;
                actModel.status = 1;
               
                actModel.canyuTT = 0;
                actModel.toupiaoTT = 0;
                actModel.liulanTT = 0;
                //1新增主表
                id = actBll.Add(actModel);
 
              
                AddAdminLog(MXEnums.ActionEnum.Add.ToString(), "添加投票活动，主键为" + id); //记录日志//1e2124dd04e11d01b9df2865f85944be
                JscriptMsg("添加投票活动成功！", "action_list.aspx");
            }
            else
            {   //修改
                //1修改主表
                actBll.Update(actModel);
                AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "修改投票活动，主键为" + id); //记录日志
                JscriptMsg("修改投票活动成功！", "action_list.aspx");
            }

        }

       
    }
}